package ysq.xyg.util;

import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * @author taocongcong
 * @create 2018-09-20 14:37
 */
public class JsonReqClient extends AbsRestClient {

    public String ucpaasAccountSid = ProProperties.getInstance().getUcpaasAccountSid();
    public String ucpaasAppId = ProProperties.getInstance().getUcpaasAppId();
    public String ucpaasAuthToken = ProProperties.getInstance().getUcpaasAuthToken();
    public String ucpaasTemplateId = ProProperties.getInstance().getUcpaasTemplateId();

    public String sendSms(String mobile, String param) {

        String result = "";

        try {
            String url = getStringBuffer().append("/sendsms").toString();
            Map<String, String> hashMap = new HashMap<String, String>();
            hashMap.put("sid", ucpaasAccountSid);
            hashMap.put("token", ucpaasAuthToken);
            hashMap.put("appid", ucpaasAppId);
            hashMap.put("templateid", ucpaasTemplateId);
            hashMap.put("param", param);
            hashMap.put("mobile", mobile);
            hashMap.put("uid", "");
            result = HttpUtils.post(url, hashMap);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public String sendSms(String sid, String token, String appid, String templateid, String param, String mobile,
                          String uid) {

        String result = "";

        try {
            String url = getStringBuffer().append("/sendsms").toString();
            Map<String, String> hashMap = new HashMap<String, String>();
            hashMap.put("sid", sid);
            hashMap.put("token", token);
            hashMap.put("appid", appid);
            hashMap.put("templateid", templateid);
            hashMap.put("param", param);
            hashMap.put("mobile", mobile);
            hashMap.put("uid", uid);
            HttpUtils.post(url, hashMap);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public String sendSmsBatch(String sid, String token, String appid, String templateid, String param, String mobile,
                               String uid) {

        String result = "";

        try {
            String url = getStringBuffer().append("/sendsms_batch").toString();

            Map<String, String> hashMap = new HashMap<String, String>();
            hashMap.put("sid", sid);
            hashMap.put("token", token);
            hashMap.put("appid", appid);
            hashMap.put("templateid", templateid);
            hashMap.put("param", param);
            hashMap.put("mobile", mobile);
            hashMap.put("uid", uid);
            HttpUtils.post(url, hashMap);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public String addSmsTemplate(String sid, String token, String appid, String type, String template_name,
                                 String autograph, String content) {

        String result = "";

        try {
            String url = getStringBuffer().append("/addsmstemplate").toString();

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("sid", sid);
            jsonObject.put("token", token);
            jsonObject.put("appid", appid);
            jsonObject.put("type", type);
            jsonObject.put("template_name", template_name);
            jsonObject.put("autograph", autograph);
            jsonObject.put("content", content);

            String body = jsonObject.toJSONString();

            System.out.println("body = " + body);

            // result = HttpClientUtil.postJson(url, body, null);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public String getSmsTemplate(String sid, String token, String appid, String templateid, String page_num,
                                 String page_size) {

        String result = "";

        try {
            String url = getStringBuffer().append("/getsmstemplate").toString();

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("sid", sid);
            jsonObject.put("token", token);
            jsonObject.put("appid", appid);
            jsonObject.put("templateid", templateid);
            jsonObject.put("page_num", page_num);
            jsonObject.put("page_size", page_size);

            String body = jsonObject.toJSONString();

            System.out.println("body = " + body);

            // result = HttpClientUtil.postJson(url, body, null);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public String editSmsTemplate(String sid, String token, String appid, String templateid, String type,
                                  String template_name, String autograph, String content) {

        String result = "";

        try {
            String url = getStringBuffer().append("/editsmstemplate").toString();

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("sid", sid);
            jsonObject.put("token", token);
            jsonObject.put("appid", appid);
            jsonObject.put("templateid", templateid);
            jsonObject.put("type", type);
            jsonObject.put("template_name", template_name);
            jsonObject.put("autograph", autograph);
            jsonObject.put("content", content);

            String body = jsonObject.toJSONString();

            System.out.println("body = " + body);

            // result = HttpClientUtil.postJson(url, body, null);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public String deleterSmsTemplate(String sid, String token, String appid, String templateid) {

        String result = "";

        try {
            String url = getStringBuffer().append("/deletesmstemplate").toString();

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("sid", sid);
            jsonObject.put("token", token);
            jsonObject.put("appid", appid);
            jsonObject.put("templateid", templateid);

            String body = jsonObject.toJSONString();

            System.out.println("body = " + body);

            // result = HttpClientUtil.postJson(url, body, null);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

}
